package com.opensistemas.spm.model;

import org.appfuse.model.BaseObject;

import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;

import java.io.Serializable;

@Entity
@Table(name="workflow")
@Inheritance(strategy= InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="workflow_type",  discriminatorType = DiscriminatorType.STRING)
abstract public class Workflow extends BaseObject implements Serializable {
    private Long id;
    private Project project;
    private Float duration;
    private String workflowType;
    private Set<WorkflowUnit> workflowUnits = new HashSet<WorkflowUnit>(0);

    @Id  @GeneratedValue(strategy = GenerationType.AUTO)    
    public Long getId() {
        return this.id;
    }
    
    public void setId(Long id) {
        this.id = id;
    }
@ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="project_id", nullable=false)
    public Project getProject() {
        return this.project;
    }
    
    public void setProject(Project project) {
        this.project = project;
    }
    
    @Column(name="duration", nullable=false, precision=12, scale=0)
    public float getDuration() {
        return this.duration;
    }
    
    public void setDuration(float duration) {
        this.duration = duration;
    }
    
    @Column(name="workflow_type", nullable=false, length=50, insertable=false, updatable = false)
    public String getWorkflowType() {
        return this.workflowType;
    }
    
    public void setWorkflowType(String workflowType) {
        this.workflowType = workflowType;
    }
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="workflow")
    public Set<WorkflowUnit> getWorkflowUnits() {
        return this.workflowUnits;
    }
    
    public void setWorkflowUnits(Set<WorkflowUnit> workflowUnits) {
        this.workflowUnits = workflowUnits;
    }

    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Workflow pojo = (Workflow) o;

        if (project != null ? !project.equals(pojo.project) : pojo.project != null) return false;
        if (duration != null ? !duration.equals(pojo.duration) : pojo.duration != null) return false;
        if (workflowType != null ? !workflowType.equals(pojo.workflowType) : pojo.workflowType != null) return false;

        return true;
    }

    public int hashCode() {
        int result = 0;
        result = (project != null ? project.hashCode() : 0);
        result = 31 * result + (duration != null ? duration.hashCode() : 0);
        result = 31 * result + (workflowType != null ? workflowType.hashCode() : 0);

        return result;
    }

    public String toString() {
        StringBuffer sb = new StringBuffer(getClass().getSimpleName());

        sb.append(" [");
        sb.append("id").append("='").append(getId()).append("', ");
        sb.append("project").append("='").append(getProject()).append("', ");
        sb.append("duration").append("='").append(getDuration()).append("', ");
        sb.append("workflowType").append("='").append(getWorkflowType()).append("', ");
        
        sb.append("]");
      
        return sb.toString();
    }

}
